package com.sbl.bajw.pc.service;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.AuditModelDao;
import com.sbl.bajw.mapper.CameraHouseModelDao;
import com.sbl.bajw.mapper.CameraModelDao;
import com.sbl.bajw.mapper.PrizeAlarmModelDao;
import com.sbl.bajw.pc.model.AuditModel;
import com.sbl.bajw.pc.model.CameraHouseModel;
import com.sbl.bajw.pc.model.CameraModel;
import com.sbl.bajw.pc.model.PrizeAlarmModel;

@Service
public class AuditModelService {

	@Resource
	private AuditModelDao dao;
	
	@Resource
	private CameraModelDao camera_dao;
	
	@Resource
	private CameraHouseModelDao house_dao;
	
	@Resource
	private PrizeAlarmModelDao alarm_dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	@Resource
	private CameraHouseService cameraHouse_service;
	
	public void insert(AuditModel model){
		dao.insert(model);
	}
	
	@Transactional(readOnly = false)
	public void auditCamera(AuditModel model){
		model.setCreater(SessionUnit.getUserId());
		CameraModel camera = new CameraModel();
		camera.setId(model.getBusinessId());
		camera.setIsQuelity(model.getIsQuality());
		camera.setUpdater(model.getCreater());
		camera_dao.update(camera);
		insert(model);
	}
	
	@Transactional(readOnly = false)
	public void auditCameraHouse(AuditModel model) throws Exception{
		model.setCreater(SessionUnit.getUserId());
		CameraHouseModel cameraHouse = house_dao.getModel(model.getBusinessId());
		if(model.getIsQuality().equals("1")){
			if(cameraHouse.getPoliceStation() == null || cameraHouse.getPoliceStation().trim().equals("")){
				throw new MessageException("所属派出所不能为空");
			}
			String code = cameraHouse_service.getCameraHouseCode(cameraHouse.getPoliceStation());
			cameraHouse.setCode(code);
		}
		cameraHouse.setId(model.getBusinessId());
		cameraHouse.setIsQuelity(model.getIsQuality());
		cameraHouse.setUpdater(model.getCreater());
		house_dao.update(cameraHouse);
		insert(model);
	}
	
	@Transactional(readOnly = false)
	public void processAlarm(AuditModel model){
		model.setCreater(SessionUnit.getUserId());
		PrizeAlarmModel alarm = new PrizeAlarmModel();
		alarm.setId(model.getBusinessId());
		alarm.setIsQuelity(model.getIsQuality());
		alarm.setLastBy(model.getCreater());
		alarm_dao.update(alarm);
		insert(model);
	}
	
	public String getList(HashMap<String, Object> parm){
		String json = gridDao.selectPage(AuditModelDao.class, parm, "getList");
		return json;
	}
}
